﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.IO;
using System.Web;

namespace VioletDevelopmentLaboratory.Net.Web.Configuration
{
	public sealed class CompilationSectionManager : ConfigurationManagerBase
	{
		private CompilationSection section;

		public CompilationSectionManager()
		{
			this.section = (CompilationSection)base.GetConfigurationSection("system.web/compilation");
		}

		public void ModifyCompilation(string assemblyPostProcessorType, bool batch, TimeSpan batchTimeout, bool debug, string defaultLanguage, int maxBatchGeneratedFileSize, int maxBatchSize, int numRecompilesBeforeAppRestart, bool strict, string tempDirectory, bool urlLinePragmas)
		{
			this.section.AssemblyPostProcessorType = assemblyPostProcessorType;
			this.section.Batch = batch;
			this.section.BatchTimeout = batchTimeout;
			this.section.Debug = debug;
			if (string.IsNullOrEmpty(defaultLanguage))
			{
				defaultLanguage = "vb";
			}
			this.section.DefaultLanguage = defaultLanguage;
			//this.section.Explicit = explicit;
			this.section.MaxBatchGeneratedFileSize = maxBatchGeneratedFileSize;
			this.section.MaxBatchSize = maxBatchSize;
			this.section.NumRecompilesBeforeAppRestart = numRecompilesBeforeAppRestart;
			this.section.Strict = strict;
			this.section.TempDirectory = tempDirectory;
			this.section.UrlLinePragmas = urlLinePragmas;
			this.Save();
		}

		public CompilationSection GetCompilation()
		{
			return this.section;
		}





		public void AddAssembly(string assembly)
		{
			this.section.Assemblies.Add(new AssemblyInfo(assembly));
			this.Save();
		}
		public void RemoveAssembly(string assembly)
		{
			this.section.Assemblies.Remove(assembly);
			this.Save();
		}
		public void ClearAssemblies()
		{
			this.section.Assemblies.Clear();

			this.Save();
		}
		public AssemblyInfo GetAssembly(string assembly)
		{
			return this.section.Assemblies[assembly];
		}
		public AssemblyCollection GetAssemblies()
		{
			return this.section.Assemblies;
		}




		public void AddBuildProvider(string extension, string type)
		{
			this.section.BuildProviders.Add(new BuildProvider(extension, type));
			this.Save();
		}
		public void ModifyBuildProvider(string extension, string type)
		{
			this.section.BuildProviders[extension].Type = type;
			this.Save();
		}
		public void RemoveBuildProvider(string extension)
		{
			this.section.BuildProviders.Remove(extension);
			this.Save();
		}
		public void ClearBuilderProviders()
		{
			this.section.BuildProviders.Clear();

			this.Save();
		}
		public BuildProvider GetBuildProvider(string name)
		{
			return this.section.BuildProviders[name];
		}
		public BuildProviderCollection GetBuildProviders()
		{
			return this.section.BuildProviders;
		}




		public void AddCodeSubDirectory(string directoryName)
		{
			DirectoryInfo dir = new DirectoryInfo(Path.Combine(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "App_Code"), directoryName));
			if (!dir.Exists)
			{
				try
				{
					dir.Create();
				}
				catch
				{
					throw;
				}
			}
			this.section.CodeSubDirectories.Add(new CodeSubDirectory(directoryName));
			this.Save();
		}
		public void RemoveCodeSubDirectory(string directoryName)
		{
			this.section.CodeSubDirectories.Remove(directoryName);
			this.Save();
		}
		public void ClearCodeSubDirectories()
		{
			this.section.CodeSubDirectories.Clear();

			this.Save();
		}
		public CodeSubDirectory GetCodeSubDirectory(int index)
		{
			return this.section.CodeSubDirectories[index];
		}
		public CodeSubDirectoriesCollection GetCodeSubDirectories()
		{
			return this.section.CodeSubDirectories;
		}





		public void AddExpressionBuilder(string expressionPrefix, string type)
		{
			this.section.ExpressionBuilders.Add(new ExpressionBuilder(expressionPrefix, type));
			this.Save();
		}
		public void ModifyExpressionBuilder(string expressionPrefix, string type)
		{
			this.section.ExpressionBuilders[expressionPrefix].Type = type;
			this.Save();
		}
		public void RemoveExpressionBuilder(string expressionPrefix)
		{
			this.section.ExpressionBuilders.Remove(expressionPrefix);
			this.Save();
		}
		public void ClearExpressionBuilders()
		{
			this.section.ExpressionBuilders.Clear();

			this.Save();
		}
		public ExpressionBuilder GetExpressionBuilder(string name)
		{
			return this.section.ExpressionBuilders[name];
		}
		public ExpressionBuilderCollection GetExpressionBuilders()
		{
			return this.section.ExpressionBuilders;
		}
	}
}